package cn.dankal.flyaudio.pop;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yzq.zxinglibrary.android.CaptureActivity;

import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.Constants;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.current.post.CurrentDetailActivity;
import cn.dankal.flyaudio.current.post.PublishActivity;
import cn.dankal.flyaudio.friend.AddFriendActivity;
import cn.dankal.flyaudio.friend.CreateGroupByPasswordActivity;
import cn.dankal.flyaudio.friend.PickContractPersonActivity;
import io.reactivex.functions.Consumer;
import razerdp.basepopup.BasePopupWindow;

public class HomeScanPop extends BasePopupWindow implements View.OnClickListener {
    public HomeScanPop(Context context) {
        super(context);
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.pop_friend);
    }

    @Override
    public void onViewCreated(View contentView) {
        super.onViewCreated(contentView);
        TextView textView = getContentView().findViewById(R.id.tv_add_friend);
        textView.setText("发此刻");
        getContentView().findViewById(R.id.tv_add_friend).setOnClickListener(this);
        getContentView().findViewById(R.id.tv_create_group).setOnClickListener(this);
        getContentView().findViewById(R.id.tv_scan).setOnClickListener(this);
        getContentView().findViewById(R.id.tv_create_group_by_password).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.tv_add_friend == id) {
            ActivityUtils.startActivity(PublishActivity.class);
            dismiss();
        } else if (R.id.tv_create_group == id) {
            ActivityUtils.startActivity(PickContractPersonActivity.class);
            dismiss();
        } else if (R.id.tv_scan == id) {
            ActivityUtils.getTopActivity().startActivityForResult(new Intent(ActivityUtils.getTopActivity(), CaptureActivity.class), Constants.SCAN);
            dismiss();
        } else if (R.id.tv_create_group_by_password == id) {
            RxPermissions rxPermissions = new RxPermissions(getContext());
            rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION).subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                    if (aBoolean) {
                        //申请的权限全部允许
                        ActivityUtils.startActivity(CreateGroupByPasswordActivity.class);
                        dismiss();
                    } else {
                        //只要有一个权限被拒绝，就会执行
                        ToastUtils.showShort("未授权权限，部分功能不能使用!");
                    }
                }
            });

        }
    }
}
